package statadminlogic

import (
	"context"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"
	"time"

	"navi-mumbai/app/rpc/stat/internal/svc"
	"navi-mumbai/app/rpc/stat/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryDataBoardLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryDataBoardLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryDataBoardLogic {
	return &QueryDataBoardLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询充值统计
func (l *QueryDataBoardLogic) QueryDataBoard(in *pb.QueryDataBoardReq) (*pb.QueryDataBoardResp, error) {
	defer utils.RecoverPanic()

	var start, end int64
	if in.StartTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start, _ = strconv.ParseInt(t.Format(dbkey.DateFormat2), 10, 64)
	}
	if in.EndTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end, _ = strconv.ParseInt(t.Format(dbkey.DateFormat2), 10, 64)
	}

	resp := &pb.QueryDataBoardResp{}
	var err error
	// 查询总充值额
	resp.DepositAmount, err = l.svcCtx.UserDepositDailyStatModel.SumAmount(l.ctx, in.PayChannelId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	//查询总提现金额
	resp.WithdrawAmount, err = l.svcCtx.UserWithdrawDailyStatModel.SumAmount(l.ctx, in.PayChannelId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	// 查询游戏列表数据
	gameList, err := l.svcCtx.GameModel.FindGameList(l.ctx)
	// 遍历所有游戏，获取所有游戏的统计数据看板
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	for _, game := range gameList {
		// 查询每个游戏下注金额
		var orderAmount int64
		orderAmount, err = l.svcCtx.UserGameDailyOrderStatModel.SumOrderAmount(l.ctx, 0, game.GameId, start, end)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		resp.TotalBet += orderAmount
		// 查询每个游戏的返彩金额
		returnAmount, err := l.svcCtx.UserGameDailyOrderStatModel.SumOrderReturnAmount(l.ctx, 0, game.GameId, start, end)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
		resp.TotalPayout += returnAmount
		// 封装每个游戏的数据
		gameStatList := &pb.GameStat{
			GameId:       game.GameId,
			OrderAmount:  decimal.NewFromInt(orderAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2),
			ReturnAmount: decimal.NewFromInt(orderAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2),
		}
		resp.List = append(resp.List, gameStatList)
	}
	return resp, nil
}
